'''
Description: 
Author: HowsenFisher
Date: 2021-04-12 09:52:30
LastEditTime: 2021-04-12 16:15:23
LastEditors: liutq
Reference: 
'''
import pyaudio,socket

class VoiceRecorder:
    def __init__(self,parent,ip,port):
        self.ip = ip
        self.port = port
        self.parent = parent
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.connect((self.ip,self.port))
        self.p = pyaudio.PyAudio()
        self.chunk_size = 1024 # 512
        self.audio_format = pyaudio.paInt16
        self.channels = 2
        self.rate = 44100
        self.record_stream = self.p.open(   format=self.audio_format,
                                            channels=self.channels,
                                            rate=self.rate,
                                            input=True,
                                            frames_per_buffer=self.chunk_size)#创建录音文件
        self.record()

    def record(self):
        while True:
            if self.parent.voiceStop:
                print("record stop")
                break
            voiceData = self.record_stream.read(self.chunk_size)
            self.s.send(voiceData)

